import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import {RollService} from "../../../../my-service/roll.service";
import {UtilService} from "../../../../my-service/util.service";

@Component({
  selector: 'start-root',
  templateUrl: './start.component.html',
  styleUrls: ['./start.component.css'],
})
export class StartComponent implements OnInit {
 rollIndex ={
   current:1,
   _this:null,//上下文环境
   userGuid:'',
   userName:'',
   showDeleteRoll:false,//回收站默认不显示
   showEndRoll: false,//默认不显示过期
   isEditor:false,//是否处于编辑状态
   isDeleteEditor:false,//是否处于回收站编辑状态
   addRoll:false,//开启添加花名册,
   isShowMore:false,//控制查看更多和收缩
   coverUrl:["url(/assets/img/rollIndex/cover1.png)"],
   myRollArr:[],//存着的要显示的
   myRollArrs:[],//存着所有有效的
   isShowMyRoll:6,//默认显示多少个
   showMyRollMore:12,//当点击查看更多没有超过12个的时候把查看更多隐藏了
   endRollArr:[],//存储过期的花名册
   deleteRollArrs:[],//存储已删除花名册
   deleteRollArr:[],//存储要显示的
   updateRoll:null,//存储修改的花名册
   showDefaultDelete:6,//回收站默认一页显示个数
   pageIndex:1,//分页页码
   pageTatol:null,//总页码
   isShowPage:false,
   isLoadIng:true,//是否出现加载画面
   onCloseAdd:function (event) {
     this.addRoll = event;
     this._this.getClassList(this.userGuid,0,null);//重新获取花名册
     if ( this.updateRoll){
       this.updateRoll = null;
     }
   },//关闭花名册
   showMyRoll:function () {
     if (this.myRollArrs.length>this.isShowMyRoll){
       this.myRollArr = this.myRollArrs.slice(0,this.isShowMyRoll);
       this.isShowMore = true;
     }else {
       this.myRollArr = this.myRollArrs;
     }
   },//改变要展示数据的多少,初始化
   showMore:function () {
     let cont = 2;
     if(this.myRollArrs.length>=this.showMyRollMore){
       this.isShowMore = true;
     }else {
       this.isShowMore = false;
     }
     if(this.myRollArrs.length>= this.isShowMyRoll*cont){
       let temp = this.myRollArrs.slice(this.isShowMyRoll*(cont-1),this.isShowMyRoll*cont);
        temp.forEach(item=>{
          this.myRollArr.push(item);
        })
     }else {
       let temp =this.myRollArrs.slice(this.isShowMyRoll*(cont-1));
       temp.forEach(item=>{
         this.myRollArr.push(item);
       })
     }
     cont++;
   },
   UpdateRoll:function (myroll) {
     this.updateRoll = myroll;
     this.addRoll = true;
   },
   pagination:function () {
       this.deleteRollArr = this.deleteRollArrs.slice(this.showDefaultDelete*(this.pageIndex-1),this.pageIndex*this.showDefaultDelete);
     //分页对数组进行切割
   },
   //分页函数
   _constructorPagination:function () {
     if(this.deleteRollArrs.length>this.showDefaultDelete){
       this.pageTatol = Math.ceil(this.deleteRollArrs.length/this.showDefaultDelete);
       this.deleteRollArr = this.deleteRollArrs.slice(0,this.showDefaultDelete);
       this.pageIndex = 1;
       this.isShowPage = true;
     }else {
       this.deleteRollArr = this.deleteRollArrs;
     }
   }//当回收站中数据改变时对分页器进行改变
 };
  constructor(
    private router: Router,
    private message: NzMessageService,
    private routeInfo:ActivatedRoute,
    private rollServe:RollService,
    private utilServe:UtilService
  ) { }
   getClassList(userGuid,vaild,status):void{
     this.rollServe.getClassList(userGuid,vaild,status).then(res=>{
      if (res.status == 1){
        this.rollIndex.isLoadIng = false;
        this.rollIndex.myRollArrs = res.data;
        this.rollIndex.showMyRoll();
      }else {
        console.log(res);
        this.message.error('服务器内部错误，请刷新重试');
      }
    })
   }
  getUserInfo(userGuid):void{
    this.rollServe.getUserName(userGuid).then(res=>{
      if (res.status==1){
        this.rollIndex.userName = res.data.UserName;
      }else {
        console.log(res);
        this.message.error('服务器内部错误，请刷新重试');
      }
    })
  }
  getUserGuid() {
    this.utilServe.getUserInfo().then(res=>{
      this.rollIndex.userGuid = res.userGuid;
      this.getUserInfo(this.rollIndex.userGuid);
      this.getClassList(this.rollIndex.userGuid,0,null);
    })
  }
  ngOnInit() {
       this.getUserGuid();
      this.rollIndex._this = this;
      let _this = this;
      window.onresize=function () {
        if(this.document.body.clientWidth <= 1245){
          _this.rollIndex.isShowMyRoll=4;
          _this.rollIndex.showMyRoll();
          _this.rollIndex.showDefaultDelete = 4;
         _this.rollIndex._constructorPagination();
        }
      }
  }
  goToClassIndex(classGuid) {
    if(this.rollIndex.isEditor){
      return
    }else {
  this.router.navigate(['../info',classGuid],{ relativeTo: this.routeInfo });
    }
  }
  getEndClassList() {
    this.rollIndex.showEndRoll = !this.rollIndex.showEndRoll;
    if(this.rollIndex.showEndRoll && !this.rollIndex.endRollArr[0]){
      this.rollServe.getClassList(this.rollIndex.userGuid,99,null).then(res=>{
        if (res.status == 1){
          this.rollIndex.endRollArr = res.data;
        }else {
          console.log(res);
          this.message.error('服务器内部错误，请刷新重试');
        }
      })
    }
  }
  getDeleteClassList() {
    this.rollIndex.showDeleteRoll =! this.rollIndex.showDeleteRoll;
    if(this.rollIndex.showDeleteRoll && !this.rollIndex.deleteRollArr[0]){
      this.rollServe.getClassList(this.rollIndex.userGuid,null,99).then(res=>{
        if(res.status == 1){
          this.rollIndex.deleteRollArrs = res.data;
           this.rollIndex. _constructorPagination();
        }else {
          console.log(res);
          this.message.error('服务器内部错误，请刷新重试');
        }
      })
    }
  }
  changClassStatus(classGuid,status,index) {
    status = {State:status};
     if(status.State == 99){
       this.utilServe.comfirm('您确认要删除该花名册么',()=> {
         this.rollServe.changClassStatus(classGuid, status).then(res => {
           if (res.status == 1) {
             //重新获取数据
             this.message.success('删除花名册成功');
             this.rollIndex.myRollArrs.splice(index, 1);
             this.rollIndex.showMyRoll();
             this.rollServe.getClassList(this.rollIndex.userGuid,null,99).then(res=>{
               if(res.status == 1){
                 this.rollIndex.deleteRollArrs = res.data;
                 this.rollIndex. _constructorPagination();
               }else {
                 console.log(res);
                 this.message.error('服务器内部错误，请刷新重试');
               }
             })
           }
         })
       })
     } else {
       this.utilServe.comfirm('您确认要恢复该花名册么',()=>{
           this.rollServe.changClassStatus(classGuid,status).then(res=>{
             if(res.status == 1) {
               //重新获取数据
               this.message.success('恢复花名册成功');
               let myRoll = this.rollIndex.deleteRollArrs.splice(index,1);
               this.rollIndex. _constructorPagination();
               this.rollIndex.myRollArrs.push(myRoll[0]);
               this.rollIndex.showMyRoll();
             }else {
               console.log(res);
               this.message.error('服务器内部错误，请刷新重试');
             }
           })
         }
         )
     }
  }
}
